package com.xsq.rpc.consumer.task;

import com.xsq.rpc.common.model.RpcMapper;
import com.xsq.rpc.consumer.listener.PathChildrenListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @ClassName ScheduTask
 * @Description TODO
 * @Author xsq
 * @Date 2020/6/10 20:19
 **/
@Component
public class ScheduTask {
    /**
     * 5秒执行一次,清除超过5秒的服务端相应时间
     */
    @Scheduled(cron = "*/5 * * * * *")
    public void run() {
        Map<String, RpcMapper> rpcmappers = PathChildrenListener.RPCMAPPERS;
        if (CollectionUtils.isEmpty(rpcmappers)) {
            return;
        }
        for (Map.Entry<String, RpcMapper> map : rpcmappers.entrySet()) {
            RpcMapper value = map.getValue();
            Long time = value.getTime();
            if (time != null) {
                Long second = (System.currentTimeMillis() - time) / 1000;
                //超过5秒,响应时间过期进行清除
                if (second > 5) {
                    System.out.println("定时清除超时5秒的服务端相应时间path：" + map.getKey());
                    value.setTime(null);
                    value.setResponseTime(null);
                }
            }
        }
    }
}
